/**
 *    Copyright (C) 2008, 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

package ru.teleology.cubefs.editor.hex;

import javax.swing.table.AbstractTableModel;

import ru.teleology.cubefs.api.models.Page;
import ru.teleology.cubefs.editor.model.TreePageMetadataHelper;

public class HexTableModel extends AbstractTableModel {

	private Page page;
	private TreePageMetadataHelper metadata;
	
	private int columns = 32;
	
	private int pageSize = 0;
	private boolean edit = false;
	
	public HexTableModel() {
		
	}
	
	@Override
	public int getColumnCount() {
		return columns + 1;
	}

	@Override
	public int getRowCount() {
		int rows = (int) HexHelper.idiv(pageSize, columns);
		if (rows == 0) rows = 1;
		
		return page !=null ? rows : 0;
	}
	
	@Override
	public String getColumnName(int column) {
		return column == 0 ? "Addr" : HexHelper.asHex(column - 1, 2);
	}
	
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		if (!edit) {
			return false;
		}
		else if (columnIndex > 0) {
			int idx = rowIndex * columns + columnIndex - 1;
			return idx < pageSize;
		}
		else {
			return false;
		}
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		if (columnIndex == 0) {
			return HexHelper.asHex(rowIndex * columns, 4);
		}
		else if (page != null) {
			int idx = rowIndex * columns + columnIndex - 1;
			if (idx < pageSize) {
				int value = page.GetByte(idx);
				return HexHelper.asHex(value, 2);
			}
			else {
				return null;
			}
		}
		else {
			return null;
		}
	}
	
	@Override
	public void setValueAt(Object value, int rowIndex, int columnIndex) {
		if (columnIndex > 0 && page != null) {
			int idx = rowIndex * columns + columnIndex - 1;
			if (idx < pageSize) {
				int ival = HexHelper.toInt((String)value); 
				if (ival >= 0 && ival < 256) {
					page.SetByte(idx, ival);
				}
			}
		}
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page, TreePageMetadataHelper metadata) {
		this.page 		= page;
		this.metadata 	= metadata;
		
		if (page != null) {
			pageSize = metadata.getPage().GetPageDataBlockSize(page);
		}
		else {
			pageSize = 0;
		}
		
		this.fireTableDataChanged();
	}

	public void setColumns(int columns) {
		this.columns = columns;
		this.fireTableStructureChanged();
	}

	public int getColumns() {
		return columns;
	}

	public TreePageMetadataHelper getMetadata() {
		return metadata;
	}

	public void setEditMode(boolean selected) {
		this.edit  = selected;
	}

	public boolean isEdit() {
		return edit;
	}
}